﻿using System.Collections.Generic;
using System.Data.Objects;
using System.Linq;
using YawetagLib.Linq;

namespace YawetagLib.Data.L2EF
{
    internal class ObjectContextDataSource<TEntity> : IDataSource<TEntity> where TEntity : class 
    {
        public ObjectContextDataSource(IQueryable<TEntity> dataSet)
        {
            ObjectSet = (ObjectSet<TEntity>)dataSet;
        }

        public TEntity Get(int entityID)
        {
            return ObjectSet.ToExtendedSet().GetByID(entityID, Extensions.IsObjectContextEntityPrimaryKey);
        }

        public TEntity GetOrDefault(int entityID)
        {
            return ObjectSet.ToExtendedSet().GetOrDefaultByID(entityID, Extensions.IsObjectContextEntityPrimaryKey);
        }

        public IEnumerable<TEntity> GetAll()
        {
            return ObjectSet;
        }

        public void Insert(TEntity entity)
        {
            ObjectSet.AddObject(entity);
        }

        public void Update(TEntity entity)
        {
        }

        public void Delete(TEntity entity)
        {
            ObjectSet.DeleteObject(entity);
        }

        private ObjectSet<TEntity> ObjectSet { get; set; }
    }
}
